<template>
  <div>
    <p class="p1"><b @click="headClick">X </b><span>搜索商品</span></p>
    <div class="box">
      <p class="p2">
        <input
          type="text"
          placeholder="添加商品"
          v-model="value"
          @change="headChange"
        />
        <van-icon class="tu" name="clear" @click="headle" />
      </p>
    </div>

    <ul v-if="listhot.length">
      <li v-for="item in listhot" :key="item.title" >
        <van-checkbox
          v-model="item.flag"
          checked-color="green"
          @change="headchen(item.id)"
        ></van-checkbox>
        <img :src="item.img" alt="" />
        <span>{{ item.title }}</span>
      </li>
    </ul>
    <p v-else>请输入关键词</p>
    <div class="bottom" v-if="data.length">
      <p class="num" v-for="item in data" :key="item.title">
        <img :src="item.img" alt="" /><van-icon
          :name="item.icon"
          @click="deleteclick(item.id)"
        />
      </p>
      <p class="num1" @click="JumpClick">完成({{ heack }})</p>
    </div>
    <div class="bottom" v-else>
      <p class="selected">暂无已选商品</p>
      <p class="num2">完成</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      dellid: "",
      listhot: [],
      list: [
        {
          id: 1,
          flag: false,
          img: "https://img1.baidu.com/it/u=3955766939,2686488960&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=666",
          title: "山东栖霞苹果12颗(单果约200g)",
          icon: "clear",
        },
        {
          id: 2,
          flag: false,
          img: "https://img0.baidu.com/it/u=525494518,2059778177&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
          title: "鸭梨11颗",
          icon: "clear",
        },
      ],
      data: [
        // {
        //   img:'https://img1.baidu.com/it/u=3955766939,2686488960&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=666',
        //   title:"山东栖霞苹果12颗(单果约200g)",
        //   icon:'clear',
        // }
      ],
    };
  },
 
  methods: {
    headClick() {
      this.$router.go(-1)

    },
    headChange() {
      this.listhot = this.list.filter((item) =>
        item.title.includes(this.value)
      );
      console.log(this.listhot);
      this.headchen()
    },
    headle() {
      this.value = "";
      this.listhot = "";
    },
    JumpClick() {
      this.$router.push("/clubs/Participate");
      this.value = "";
      this.data=[]
      this.$store.commit("SET_JUMPCLICK",Array.from(new Set([...this.$store.state.listhot,...this.listhot])))
  
      // this.$store.commit("SET_JUMPCLICK",[...this.$store.state.listhot,...this.listhot])
      console.log(this.$store.state.listhot);
      this.listhot=[]
    },
    headchen(id) {
      console.log(id); 
      this.dellid = id;
      this.data = [];
      this.listhot.map((item) => {
        if (item.flag == true){
          this.data.push({
            img: item.img,
            icon: item.icon,
          });
        } else {
          this.value = "";
          item.flag=false
        }
      });
    },
    deleteclick(id) {
      console.log(id);
      this.list.map((item) => {
        if (item.id == id) {
          item.flag = false;
        }
        return item;
      });
    },
  },
  computed: {
    heack() {
      return this.listhot.length;
    },
  },
};
</script>

<style scoped lang="scss">
.p1 {
  margin-top: 10px;
  text-align: left;
  width: 100%;
  box-sizing: border-box;
  padding: 0 10px;
  span {
    margin-left: 130px;
    font-size: 18px;
  }
}
.box {
  width: 100%;
  height: 70px;
  // position: fixed;
  // top: 10px;
  // left: 0;
  // right: 0;
  .p2 {
    height: 30px;
    input {
      border: none;
      padding: 10px 20px;
    }
    .tu {
      margin-left: 100px;
    }
  }
}
p {
  text-align: center;
}
li {
  height: 60px;
  line-height: 60px;
  display: flex;
  border-bottom: 1px solid #ccc;
  padding: 10px;
  text-align: center;
  img {
    width: 50px;
    height: 50px;
    margin: 0 10px;
  }
}
.bottom {
  width: 100%;
  height: 60px;
  line-height: 60px;
  position: absolute;
  bottom: 50px;
  display: flex;
  img {
    display: inline-block;
    width: 50px;
    height: 50px;
  }
  .van-icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .num {
    position: relative;
    width: 50px;
    height: 50px;
  }
  .num1 {
    width: 80px;
    height: 40px;
    line-height: 40px;
    border-radius: 20px;
    background: green;
    color: #fff;
    text-align: center;
    margin-left: 240px;
  }
  .num2 {
    width: 200px;
    height: 40px;
    line-height: 40px;
    border-radius: 20px;
    background: #ccc;
    color: #fff;
    text-align: center;
    margin-left: 180px;
  }
}
.selected {
  width: 200px;
  color: #333;
}
</style>